package com.fire.control.system.module.company.controller.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fire.control.system.dto.UserDTO;
import com.fire.control.system.module.common.controller.BaseController;
import com.fire.control.system.module.company.service.CompanyService;
import com.fire.control.system.module.company.service.IManagementService;
import com.fire.control.system.standard.BusinessInterfaceStandard;
import com.fire.control.system.statics.Message_Response;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.management.ManagementRequestProtocol;

/**
 * @author liuxincheng
 * @ClassName: ManagementUnitCtrol
 * @Description: TODO(单位控制器)
 * @author A18ccms a18ccms_gmail_com
 * @date 2017年5月9日 下午2:10:31
 */
@RestController
@RequestMapping("/web/managementCtr")
public class ManagementUnitCtrol extends BaseController
		implements BusinessInterfaceStandard<ManagementRequestProtocol, ResponseProtocol> {

	@Autowired
	private IManagementService managementService;

	@Autowired
	private ProtocolSelector protocolSelector;
	
	@Autowired
	private CompanyService companyService;

	/**
	 * @author liuxincheng
	 * @Title: getManufacturingCompanyList
	 * @Description: TODO(制造单位列表查询)
	 * @param @param
	 *            requestProtocol
	 * @param @param
	 *            p
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "getManufacturingCompanyList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getManufacturingCompanyList(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = this.getManagementListByCondition(requestProtocol);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: getUserCompanyList
	 * @Description: TODO(使用单位列表查询)
	 * @param @param
	 *            requestProtocol
	 * @param @param
	 *            p
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型 Operating Units
	 * @throws null
	 */
	@RequestMapping(path = "getUserCompanyList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getUserCompanyList(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = this.getManagementListByCondition(requestProtocol);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: getOperatingUnitsList
	 * @Description: TODO(运营单位列表查询)
	 * @param @param
	 *            requestProtocol
	 * @param @param
	 *            p
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "getOperatingUnitsList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getOperatingUnitsList(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = this.getManagementListByCondition(requestProtocol);
		return response;
	}

	/**
	 * @author liuxincheng @Title: getManagementListByCondition @Description:
	 *         TODO(通过条件查询单位列表) @param @param requestProtocol @param @return
	 *         设定文件 @return ResponseProtocol 返回类型 @throws
	 */
	@RequestMapping(path = "getManagementListByCondition", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getManagementListByCondition(ManagementRequestProtocol requestProtocol) {
		UserDTO u = this.getUser();
		List<String> childrenCompanyIdList = companyService.getChildCompanyIdList(u.getCompany().getId());
		requestProtocol.setCompanyIds(childrenCompanyIdList);
		ResponseProtocol response = managementService.getManagementListByCondition(requestProtocol);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: getDeviceGroups
	 * @Description: TODO(获取设备类型集合)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "getDeviceTypeList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getDeviceTypeList(ManagementRequestProtocol requestProtocol, HttpServletRequest request) {
		ResponseProtocol response = managementService.getDeviceTypeList(requestProtocol, request);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: getUserListByCompanyId
	 * @Description: TODO(根据企业Id获取用户列表)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "getUserListByCompanyId", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getUserListByCompanyId(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = managementService.getUserListByCompanyId(requestProtocol);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: getParentCompany
	 * @Description: TODO(获得上层单位)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/getParentCompany", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getParentCompany(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = managementService.getParentCompany(requestProtocol, this.getUser());
		return response;
	}

	/**
	 * @author liuxincheng @Title: saveManagementInfo @Description:
	 *         TODO(添加单位信息) @param @param requestProtocol @param @return
	 *         设定文件 @return ResponseProtocol 返回类型 @throws
	 */
	@RequestMapping(path = "saveManagementInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol saveManagementInfo(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = managementService.saveManagementInfo(requestProtocol, this.getUser());
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: deleteManagementInfo
	 * @Description: TODO(删除选中的单位)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "deleteManagementInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol deleteManagementInfo(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);

		if (StringUtils.isEmpty(requestProtocol.getId()) == true) {
			ResponseMessage.CODE_400.messageToProtocol(response).setMessage("单位删除失败!");
		} else {
			response = managementService.deleteManagementInfo(requestProtocol);
			ResponseMessage.CODE_200.messageToProtocol(response).setMessage("单位删除成功!");
		}
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: getManagementDetail
	 * @Description: TODO(获得选中单位)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "getManagementDetail", method = RequestMethod.POST)
	public ResponseProtocol getManagementDetail(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		response = managementService.getCheckManagementList(requestProtocol);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: updateManagementInfo
	 * @Description: TODO(修改单位信息)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "updateManagementInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol updateManagementInfo(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		response = managementService.updateManagementInfo(requestProtocol, this.getUser());
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: updateManagementStatus
	 * @Description: TODO(启用状态)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "updateManagementStatus", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol updateManagementStatus(ManagementRequestProtocol requestProtocol) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		response = managementService.updateManagementStatus(requestProtocol);
		return response;
	}
}
